package com.sky.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sky.exception.BaiduApiException;
import com.sky.exception.BaseException;
import com.sky.exception.OrderBusinessException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@Slf4j
public class BaiduUtil {
    private static final String locationApi = "https://api.map.baidu.com/geocoding/v3";
    private static final String distanceApi = "https://api.map.baidu.com/directionlite/v1/driving";
    private String address;
    private String ak;
    private String output;

    public String getblocation() {
        HashMap<String, String> map = new HashMap<>();
        String location = "default";
        map.put("address", address);
        map.put("ak", ak);
        map.put("output", output);
        try {
            location = HttpClientUtil.doGet(locationApi, map);
        } catch (Exception e) {

        }
        return location;
    }

    public String getulocation(Map<String, String> umap) {
        String location = "default";
        umap.put("ak", ak);
        umap.put("output", output);
        try {
            location = HttpClientUtil.doGet(locationApi, umap);
        } catch (Exception e) {

        }
        return location;
    }

    public String getdistance(Map<String, String> dmap) {
        String distance = "default";
        dmap.put("ak", ak);
        dmap.put("output", output);
        try {
            distance = HttpClientUtil.doGet(distanceApi, dmap);
        } catch (Exception e) {

        }
        return distance;
    }

    private Integer checkOutOfRange(String address[]) {
        String blocations = getblocation();
        JSONObject jsonObject = JSON.parseObject(blocations);
        if (!jsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("店铺地址解析失败");
        }

        //数据解析
        JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
        String lat = location.getString("lat");
        String lng = location.getString("lng");
        //店铺经纬度坐标
        String shopLngLat = lat + "," + lng;
        Map<String, String> map = new HashMap<>();
        map.put("address", address[0]);
        //获取用地址2的经纬度坐标
        String ulocations = getulocation(map);

        jsonObject = JSON.parseObject(ulocations);
        if (!jsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("收货地址解析失败");
        }

        //数据解析
        location = jsonObject.getJSONObject("result").getJSONObject("location");
        lat = location.getString("lat");
        lng = location.getString("lng");
        //用户收货地址经纬度坐标
        String userLngLat = lat + "," + lng;

        map.put("origin", shopLngLat);
        map.put("destination", userLngLat);
        map.put("steps_info", "0");

        //路线规划
        String json = getdistance(map);

        jsonObject = JSON.parseObject(json);
        if (!jsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("配送路线规划失败");
        }

        //数据解析
        JSONObject result = jsonObject.getJSONObject("result");
        JSONArray jsonArray = (JSONArray) result.get("routes");
        Integer distance = (Integer) ((JSONObject) jsonArray.get(0)).get("distance");

        return distance;
    }

}
